<template>
	<view class="pro_wrap">
		
		<view>测试进度：</view>
		<progress stroke-width="8" activeColor="#00ff00" :percent="percent" ></progress> 
		<text>{{selIndex+1}}/{{quesetList.length}}</text>
	</view>
	<view class="test_one"  v-show="index==selIndex" v-for="(item,index) in quesetList"  > 
		<view class="quest">
			<view>{{index+1}}</view> <view>{{item.topic}}</view>
		</view>
		<view class="my_answer">
			<radio-group @change="radioChange">
					<view class="radio"  v-for="aitem in item.options">
				      <radio :value="aitem.son_id"   />{{aitem.title}}
				    </view>
			</radio-group>
		</view>
	</view>
	<view v-if="selIndex==quesetList.length">
		
			<button type="primary" @click="toTest">确认提交问卷</button>
	</view>
	<view v-if="testEnd">
		<view>{{testEnd.scope}}：{{testEnd.result}}</view>
		<view>
			<view>{{testEnd.suggest}}</view>
		</view>
		<view>测评结果概述</view>
		<view v-for="(item,index) in testEnd.outline">{{index+1}}.{{item}}</view>
	     <view v-for="item in testEnd.recommend">
			 {{item.hospital}}--{{item.dep_name}}
			 <button type="primary">去挂号</button>
		 </view>
	</view>
</template>

<script setup>
	import {ref} from 'vue'
	import {getTestApi,getAnswerApi,getInsomniaApi,getPrematureApi,getShiMian,getNan} from '@/api/home.js'
	
	import {onLoad } from '@dcloudio/uni-app'
	let testname=ref('')
	onLoad((options)=>{
		 testname.value=options.name;//接受测试题的名字
		 uni.setNavigationBarTitle({
		 	title: options.name
		 })
		 getQlist();
	})
	let quesetList=ref([]) //问题数组
	let getQlist=async ()=>{
		if( testname.value=="抑郁测评专业版"){
				let res=await getTestApi();
				console.log(res,"---------题目--------")
				quesetList.value=res.data;
		}else if(testname.value=="失眠程度测评"){
			let shimianRes=await getInsomniaApi();
			console.log(shimianRes,"-------失眠--题目--------")
			quesetList.value=shimianRes.data;
		}else{
			let nanRes=await getPrematureApi();
			quesetList.value=nanRes.data;
		}
		
	}
	let selIndex=ref(0);
	// 
	// 获取 选择的答案数组
	// 进度条的百分比
	let percent=ref(0)
	let selArr=ref([])
	let radioChange=(e)=>{
		if(selIndex.value<quesetList.value.length){
			setTimeout(()=>{
				selIndex.value++;
				
				console.log(e.detail.value,"----------")
				selArr.value.push(e.detail.value) //答案数组
				percent.value=(selIndex.value/quesetList.value.length)*100;
				console.log(percent.value,"----------")
			},1000)		
		}else{
			uni.showToast({
				title:"最后一道题"
			})
		}
		
	}
	let testEnd=ref(null)
   // 获取测试结果
   let toTest=async ()=>{
	   if(testname.value=="抑郁测评专业版"){
			let res=await getAnswerApi({
					   value:selArr.value,
			})
			console.log(res,"--------")
			testEnd.value=res.data[0]
	   }else if(testname.value=="失眠程度测评"){
		    let shiMianres=await getShiMian({
				 value:selArr.value
			})
				console.log(shiMianres,"--------")
				testEnd.value=shiMianres.data[0]
	   }else {
		   let nanRes=await getNan({
			    value:selArr.value,
		   })
		    console.log(nanRes,"--------")
			testEnd.value=nanRes.data[0]
	   }
	   
   }
	

</script>

<style lang="scss">
	.quest{
		display: flex;
		font-size: 20px;
	}
	.test_one{
		padding: 0 20px;
		box-sizing: border-box;
	}
	.my_answer{
		margin-top: 20px;
		
		view{
			margin-top: 20px;
		}
	}
	.pro_wrap{
		display: flex;
		progress{
			width: 200px;
		}
	}
</style>